package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmSmsReceiveRole;
import com.zb.entity.ZjAdmSmsTemplate;
import com.zb.entity.dto.SmsTemplateDto;
import com.zb.entity.dto.SmsTemplateListSearchDto;
import com.zb.entity.vo.SmsTemplateListVo;
import com.zb.mapper.ZjAdmSmsTemplateMapper;
import com.zb.search.SearchDto;
import com.zb.service.ZjAdmSmsReceiveRoleService;
import com.zb.service.ZjAdmSmsTemplateService;
import com.zb.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台短信模板表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-03-21
 */
@Service
public class ZjAdmSmsTemplateServiceImpl extends ServiceImpl<ZjAdmSmsTemplateMapper, ZjAdmSmsTemplate> implements ZjAdmSmsTemplateService {
    @Resource
    private ZjAdmSmsTemplateMapper admSmsTemplateMapper;
    @Resource
    private ZjAdmSmsReceiveRoleService admSmsReceiveRoleService;

    // 新增或修改短信模板
    @Override
    public void save(SmsTemplateDto smsTemplate) {
        Assert.isNull(admSmsTemplateMapper.existByCode(smsTemplate.getId(), smsTemplate.getCode()), "短信模板编号已存在："+ smsTemplate.getCode());
        if (smsTemplate.getUpdateReceiveRole()) {
            Assert.notEmpty(smsTemplate.getRoleType(), "接收短信的角色类型不能为空，至少选项一项");
            List<Integer> roleTypes = smsTemplate.getRoleType().stream().filter(f -> !Arrays.asList("1,2,3,4,5".split(",")).contains(f.toString())).toList();
            Assert.isTrue(CollectionUtil.isEmpty(roleTypes), "接收短信的角色类型参数不合法");
        }
        ZjAdmSmsTemplate template = new ZjAdmSmsTemplate();
        BeanUtil.copyProperties(smsTemplate, template);
        template.setDisabled(smsTemplate.getStatus());
        if (null != template.getId()) {
            this.updateById(template);
        } else {
            template.setId(IdWorker.getId());
            this.save(template);
        }
        admSmsReceiveRoleService.save(template.getId(), template.getCode(), smsTemplate.getRoleType());
    }

    // 获取短信模板列表
    @Override
    public Map<String, Object> getList(SmsTemplateListSearchDto search) {
        PageUtil.setPageLimit(search);
        long count = admSmsTemplateMapper.getListCount(search);
        if (count <= 0) {
            return PageUtil.pageList(search.getPage(), search.getLimit(), count, new ArrayList<>());
        }
        List<ZjAdmSmsTemplate> templateList = admSmsTemplateMapper.getList(search);
        List<ZjAdmSmsReceiveRole> smsReceiveRoles = admSmsReceiveRoleService.getByAdmSmsTemplateIds(templateList.stream().map(ZjAdmSmsTemplate::getId).toList());
        return PageUtil.pageList(search.getPage(), search.getLimit(),count , templateList.stream().map(template -> {
            SmsTemplateListVo vo = new SmsTemplateListVo();
            BeanUtil.copyProperties(template, vo);
            vo.setStatus(template.getDisabled());
            vo.setCreateTime(template.getGmtCreate());
            vo.setUpdateTime(template.getGmtUpdate());
            vo.setRoleTypeStr(smsReceiveRoles.stream().filter(f -> Objects.equals(f.getAdmSmsTemplateId(), template.getId())).map(roleType -> {
                return roleType.getRoleType() == 1? "客服" : roleType.getRoleType() == 2? "指定人员" : roleType.getRoleType() == 3? "服务商负责人":
                        roleType.getRoleType() == 4? "工程师" : "客户";
            }).collect(Collectors.joining("、")));
            return vo;
        }).toList());
    }

    // 获取短信模板详情
    @Override
    public SmsTemplateDto getDetail(Long id) {
        ZjAdmSmsTemplate template = this.getById(id);
        Assert.notNull(template, "短信模板参数[ " + id + " ]的数据不存在");
        SmsTemplateDto smsTemplate = new SmsTemplateDto();
        BeanUtil.copyProperties(template, smsTemplate);
        smsTemplate.setStatus(template.getDisabled());
        List<ZjAdmSmsReceiveRole> smsReceiveRoles = admSmsReceiveRoleService.getByAdmSmsTemplateId(id);
        smsTemplate.setRoleType(smsReceiveRoles.stream().map(ZjAdmSmsReceiveRole::getRoleType).toList());
        return smsTemplate;
    }

    // 批量禁用或启用短信模板
    @Override
    public void updateDisableBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        admSmsTemplateMapper.updateDisableBatchById(ids, status);
    }
}
